﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace TalkDemo
{
    public partial class Form1 : Form
    {
        private int per = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {        

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try { 
            string content = txtContent.Text.Trim();
                axWindowsMediaPlayer1.URL = "";
                if (string.IsNullOrWhiteSpace(content))
                {
                    MessageBox.Show("请输入要转换的内容！");
                }
                else
                {
                    string APP_ID = ConfigurationSettings.AppSettings["APP_ID"];
                    string API_KEY = ConfigurationSettings.AppSettings["API_KEY"];
                    string SECRET_KEY = ConfigurationSettings.AppSettings["SECRET_KEY"];                  
                    var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY);
                    client.Timeout = 60000;  // 修改超时时间                  
                    var option = new Dictionary<string, object>()
                         {  {"spd", numSpeed.Value}, // 语速 0-10
                            {"vol", numval.Value}, // 音量 0-15
                            {"per", per } // 发音人选择, 0为女声，1为男声，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女
                         };
                    var result = client.Synthesis(content, option);
                    if (result.ErrorCode == 0)  // 或 result.Success
                    {

                        string filename = "语音文件" + DateTime.Now.ToString("yyyyMMddHHmm");//文件名
                        SaveFileDialog saveDialog = new SaveFileDialog();
                        saveDialog.DefaultExt = "mp3";
                        saveDialog.Filter = "音乐文件|*.mp3";
                        saveDialog.FileName = filename;
                        saveDialog.ShowDialog();
                        filename = saveDialog.FileName;
                        if (filename.IndexOf(":") < 0) return; //被点了取消
                        FileStream file = new FileStream(saveDialog.FileName, FileMode.Create);
                        file.Write(result.Data, 0, result.Data.Count());
                        file.Close();
                        axWindowsMediaPlayer1.URL = filename;                        
                        //System.Diagnostics.Process.Start(filename); //直接打开
                    }
                    else
                    {
                        MessageBox.Show(result.ErrorMsg);
                    }
                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            comType.SelectedIndex = 0;
        }

        private void ComType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comType.SelectedIndex>=2) // 3、4 代表不同类型 
            {
                per = comType.SelectedIndex+1;
            }
            else
            {
                per = comType.SelectedIndex;
            }
            
        }
    }
}
